package com.forhappy.light3ds8.db;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.util.Log;

public class LedDBContentProvider extends ContentProvider{
	private static final String TAG = "LedDBContentProvider";

    public static final String AUTHORITY  = LedDBContentProvider.class.getName();

    private static final String PATH_DATA_BASE_INFO = LedDBHelper.TAB_DATAS_BASIC;
    private static final String PATH_DATA_BASE_INFO_CHECK_REAL = PATH_DATA_BASE_INFO + "_check_real";
    private static final String PATH_DATA_REAL = LedDBHelper.TAB_DATAS_REAL;
    private static final String PATH_CONFIG = LedDBHelper.TAB_CONFIG;


	private static final Uri URI_CONTENT_BASE = Uri.parse("content://" + LedDBContentProvider.class.getName());
	public static final Uri URI_DATA_BASIC_INFO = Uri.withAppendedPath(URI_CONTENT_BASE, PATH_DATA_BASE_INFO);
	public static final Uri URI_DATA_BASIC_INFO_CHECK_REAL = Uri.withAppendedPath(URI_CONTENT_BASE, PATH_DATA_BASE_INFO_CHECK_REAL);
	public static final Uri URI_DATA_REAL = Uri.withAppendedPath(URI_CONTENT_BASE, PATH_DATA_REAL);
	public static final Uri URI_CONFIG = Uri.withAppendedPath(URI_CONTENT_BASE, PATH_CONFIG);


    private static final int MATCH_DATA_BASIC_BASIC_INFO = 10;
    private static final int MATCH_DATA_BASIC_BASIC_INFO_CHECK_REAL = 11;
    private static final int MATCH_DATA_BASIC_BASIC_INFO_SINGLE = 12;

    private static final int MATCH_DATA_REAL = 20;
    private static final int MATCH_DATA_REAL_SINGLE = 21;
    private static final int MATCH_CONFIG = 30;

    private static final UriMatcher sMatch = new UriMatcher(UriMatcher.NO_MATCH);
    static {
    	sMatch.addURI(AUTHORITY, PATH_DATA_BASE_INFO, MATCH_DATA_BASIC_BASIC_INFO);
    	sMatch.addURI(AUTHORITY, PATH_DATA_BASE_INFO_CHECK_REAL, MATCH_DATA_BASIC_BASIC_INFO_CHECK_REAL);
    	sMatch.addURI(AUTHORITY, PATH_DATA_BASE_INFO +"/#", MATCH_DATA_BASIC_BASIC_INFO_SINGLE);

    	sMatch.addURI(AUTHORITY, PATH_DATA_REAL, MATCH_DATA_REAL);
    	sMatch.addURI(AUTHORITY, PATH_DATA_REAL + "/#", MATCH_DATA_REAL_SINGLE);
    	sMatch.addURI(AUTHORITY, PATH_CONFIG, MATCH_CONFIG);
    }

    LedDBHelper dbHelper = null;
	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		Log.d(TAG, "enter LedDBContentProvider::delete(uri:" +uri + ",selection:" + selection + ")");
		SQLiteDatabase db = dbHelper.getWritableDatabase();

		int ret = -1;

		Uri pathUri = null;
		switch (sMatch.match(uri)) {
		case MATCH_DATA_BASIC_BASIC_INFO:
			ret = db.delete(LedDBHelper.TAB_DATAS_BASIC, selection, selectionArgs);
			pathUri = URI_DATA_BASIC_INFO;
			break;
		case MATCH_DATA_REAL:
			ret = db.delete(LedDBHelper.TAB_DATAS_REAL, selection, selectionArgs);
			pathUri = URI_DATA_REAL;
			break;
		case MATCH_CONFIG:
			ret = db.delete(LedDBHelper.TAB_CONFIG, selection, selectionArgs);
			pathUri = URI_CONFIG;
			break;
		default:
			Log.e(TAG, "not match uri in delete:" + uri);
			break;
		}
		if(null != pathUri){
			getContext().getContentResolver().notifyChange(pathUri, null);
			if(!URI_CONFIG.equals(pathUri) && !URI_DATA_BASIC_INFO_CHECK_REAL.equals(pathUri)){
				getContext().getContentResolver().notifyChange(URI_DATA_BASIC_INFO_CHECK_REAL, null);
			}
		}
		return ret;
	}

	@Override
	public String getType(Uri uri) {
		return "ledData";
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		Log.d(TAG, "enter LedDBContentProvider::insert(uri:" +uri + ",values:" + values + ")");
		SQLiteDatabase db = dbHelper.getWritableDatabase();

		Uri pathUri = null;
		long inserRowId = -1;
		switch (sMatch.match(uri)) {
		case MATCH_DATA_BASIC_BASIC_INFO:
			inserRowId = db.insert(LedDBHelper.TAB_DATAS_BASIC, null, values);
			pathUri = URI_DATA_BASIC_INFO;
			break;
		case MATCH_DATA_REAL:
			inserRowId = db.insert(LedDBHelper.TAB_DATAS_REAL, null, values);
			pathUri = URI_DATA_REAL;
			break;
		case MATCH_CONFIG:
			inserRowId = db.insert(LedDBHelper.TAB_CONFIG, null, values);
			pathUri = URI_CONFIG;
			break;
		default:
			Log.e(TAG, "not match uri in insert:" + uri);
			break;
		}
		if(null != pathUri){
			getContext().getContentResolver().notifyChange(pathUri, null);
			if(!URI_CONFIG.equals(pathUri) && !URI_DATA_BASIC_INFO_CHECK_REAL.equals(pathUri)){
				getContext().getContentResolver().notifyChange(URI_DATA_BASIC_INFO_CHECK_REAL, null);
			}
		}
		return ContentUris.withAppendedId(pathUri, inserRowId);
	}

	@Override
	public boolean onCreate() {
		dbHelper = new LedDBHelper(getContext());
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		Log.d(TAG, "enter LedDBContentProvider::query(uri:" +uri + ",selection:" + selection +
						"selectionArgs:" + selectionArgs + ")");
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		Cursor cr = null;
		switch (sMatch.match(uri)) {
		case MATCH_DATA_BASIC_BASIC_INFO:
			cr = db.query(LedDBHelper.TAB_DATAS_BASIC, projection, selection, selectionArgs, null, null, sortOrder);
			break;
		case MATCH_DATA_BASIC_BASIC_INFO_CHECK_REAL:
//			//sqlite not support
//			final String sql = "select b.id as _id, b.info, if(r.id, 'downed', '') isDown " +
//							" from datas_basic b left join datas_real r " +
//							" on b.idx_datas = r.id order by b.id asc";

			final String sql = "select b.id as _id, b.idx_datas, b.info, b.userName, r.id isDown " +
							" from datas_basic b left join datas_real r " +
							" on b.idx_datas = r.id order by b.id asc";
			cr = db.rawQuery(sql, null);
			break;
		case MATCH_DATA_REAL:
			cr = db.query(LedDBHelper.TAB_DATAS_REAL, projection, selection, selectionArgs, null, null, sortOrder);
			break;
		case MATCH_DATA_REAL_SINGLE:
			String id = String.valueOf(ContentUris.parseId(uri));
			cr = db.query(LedDBHelper.TAB_DATAS_REAL, projection, "id = ?",
						new String[]{id}, null, null, sortOrder);
			break;
		default:
			Log.e(TAG, "not match uri in query:" + uri);
			break;
		}
		if(null != cr){
			cr.setNotificationUri(getContext().getContentResolver(), uri);
		}
		return cr;
	}

	@Override
	public int update(Uri uri, ContentValues values, String whereClause,
			String[] whereArgs) {
		Log.d(TAG, "enter LedDBContentProvider::update(uri:" +uri + ",values:" + values +
				"whereClause" + whereClause + ")");
		SQLiteDatabase db = dbHelper.getWritableDatabase();

		int ret = -1;
		Uri pathUri = null;
		switch (sMatch.match(uri)) {
		case MATCH_DATA_BASIC_BASIC_INFO:
			ret = db.update(LedDBHelper.TAB_DATAS_BASIC, values, whereClause, whereArgs);
			pathUri = URI_DATA_BASIC_INFO;
			break;
		case MATCH_DATA_REAL:
			ret = db.update(LedDBHelper.TAB_DATAS_REAL, values, whereClause, whereArgs);
			pathUri = URI_DATA_REAL;
			break;
		case MATCH_DATA_REAL_SINGLE:
			String id = String.valueOf(ContentUris.parseId(uri));
			ret = db.update(LedDBHelper.TAB_DATAS_REAL, values,  "id = ?", new String[]{id});
			pathUri = URI_DATA_REAL;
			break;
		case MATCH_CONFIG:
			ret = db.update(LedDBHelper.TAB_CONFIG, values, whereClause, whereArgs);
			pathUri = URI_CONFIG;
			break;
		default:
			Log.e(TAG, "not match uri in update:" + uri);
			break;
		}
		if(null != pathUri){
			getContext().getContentResolver().notifyChange(pathUri, null);
			if(!URI_CONFIG.equals(pathUri) && !URI_DATA_BASIC_INFO_CHECK_REAL.equals(pathUri)){
				getContext().getContentResolver().notifyChange(URI_DATA_BASIC_INFO_CHECK_REAL, null);
			}
		}
		return ret;
	}

}
